﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Fusion
{
    /// <summary>
    /// Structure describing a celsheet.
    /// Also provides static- sheet-slicing functionality
    /// </summary>
    public class TileSheet
    {
        #region Fields

		public Image image;
		public List<int> collisionIndices = new List<int>();

        public int celWidth;
        public int celHeight;
        public int columns;
        public int rows;


        public Vector2 Size
        { get { return new Vector2(celWidth, celHeight); } }

        #endregion

        #region Constructors

        /// <summary> Calculates properties based on cels </summary>
        /// <param name="Texture">Texture to reference</param>
        /// <param name="cels">Number of cels per side</param>
        public TileSheet( string textureName, int side)
		{
			image = new Image( textureName );
            celWidth = image.Width / side;
            celHeight = image.Height / side;
            columns = side;
            rows = side;
        }

        /// <summary> Calculates Width/Height based on Columns/Rows </summary>
        /// <param name="Texture"></param>
        /// <param name="Columns"></param>
        /// <param name="Rows"></param>
		public TileSheet( string textureName, int Width, int Height )
		{
			image = new Image( textureName );
			celWidth = (int)MathHelper.Clamp( Width, 0, image.Width );
			celHeight = (int)MathHelper.Clamp( Height, 0, image.Height ); ;
			columns = image.Width / celWidth;
			rows = image.Height / celHeight;
        }

        #endregion

		#region Acessors

		/// <summary> Returns a frame at specified index </summary>
        public Rectangle Frame(int index)
        {
            return TileSheet.Frame(index, columns, celWidth, celHeight);
		}

		public int CollisionIndex( int index )
		{
			if( index < 0 ) return 0;
			if( index > collisionIndices.Count ) return 0;
			return collisionIndices[index];
		}

		#endregion

		#region Static Methods

		/// <summary> Returns a frame around a specified texture </summary>
        /// <param name="index">Cel index</param>
        /// <param name="side">Number of cels per side</param>
        /// <param name="size">Width/Height of Cel</param>
        public static Rectangle Frame(int index, int side, int size)
        {
            int x = (index % side) * size;
            int y = (index / side) * size;

            return new Rectangle(x, y, size, size);
        }

        /// <summary> Returns a frame around a specified texture </summary>
        /// <param name="index">Cel index</param>
        /// <param name="side">Number of cels per side</param>
        /// <param name="width">Width of Cel</param>
        /// <param name="height">Height of Cel</param>
        public static Rectangle Frame(int index, int side, int width, int height)
        {
            if (side < 1) side = 1;
            int x = (index % side) * width;
            int y = (index / side) * height;

            return new Rectangle(x, y, width, height);
        }

        #endregion
    }
}
